﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CooStationsToolsNew.Model;
using System.Windows.Controls.Primitives;

namespace CooStationsToolsNew
{
    /// <summary>
    /// AddNewPageWin.xaml 的交互逻辑
    /// </summary>
    public partial class AddNewPageWin : Window
    {
        public static string strpageName, strpageW, strpageH;
        Popup pop = new Popup();
        public AddNewPageWin()
        {
            InitializeComponent();
            
        }
        //关闭
        private void lbtn_close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
        //确定新建
        private void NewProBt_Click(object sender, RoutedEventArgs e)
        {
            int num = 0;
            if (pageName.Text == "")
            {
                MessageForm1 mf = new MessageForm1("请填写新页名");
                mf.Owner = this;
                mf.ShowDialog();
               // MessageBox.Show("请填写新页名");
            }
            else
            {
                if (pageW.Text == "" || pageH.Text == "")
                {
                    MessageForm1 mf = new MessageForm1("请填写页面大小");
                    mf.Owner = this;
                    mf.ShowDialog();
                    //MessageBox.Show("请填写页面大小");
                }
                else
                {
                    num = 1;
                    App.cpage = new Model.CfyPage();
                    App.cpage.PageName = pageName.Text;
                    App.cpage.Height = pageH.Text;
                    App.cpage.Width = pageW.Text;
                }              
            }
            if (num == 1)
            {
                this.DialogResult = true;
                this.Close();
            }
        }
        //取消
        private void NewProCancelBt_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            pageW.Text = App.proWidth;
            pageH.Text = App.proHeight;
            pop = setPopup("不能以数字开头!",pageName);
           // pop.AllowsTransparency =
        }
        public Popup setPopup(string str,UIElement uie)
        { 
            Popup pop=new Popup();
            TextBlock tb = new TextBlock();
            ImageBrush ib=new ImageBrush();
            ib.ImageSource = new BitmapImage(new Uri("pic/popo.png", UriKind.RelativeOrAbsolute));
            ib.Stretch = Stretch.Fill;
            tb.Background=ib;
            tb.Width = 157.0;
            tb.Height = 62.0;
            tb.TextAlignment = TextAlignment.Center;
            tb.Padding = new Thickness(5.0);
            tb.Text = str;
            pop.Width = 157.0;
            pop.Height = 62.0;
            pop.Child = tb;
            pop.AllowsTransparency = true;
            pop.StaysOpen = false;
            pop.PlacementTarget = uie;
            pop.Placement = PlacementMode.Top;
            pop.PopupAnimation = PopupAnimation.Fade;
            return pop;
        }

        private void pageName_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            // System.Windows.MessageBox.Show(e.Key.ToString());
            System.Windows.Controls.TextBox txt = sender as System.Windows.Controls.TextBox;
            //屏蔽非法按键
            if ((e.Key < Key.A || e.Key > Key.Z) && txt.Text == "")
            {
                e.Handled = true;
                pop.IsOpen = true;
            }
            else if (e.Key == Key.Oem5 || e.Key == Key.OemQuestion || e.Key == Key.Divide || e.Key == Key.Multiply)
            {
                e.Handled = true;
                pop.IsOpen = true;
            }
            else if ((e.Key == Key.D8 || e.Key == Key.OemPeriod ||
                    e.Key == Key.Oem5 || e.Key == Key.Oem1 || e.Key == Key.Oem7 ||
                    e.Key == Key.OemQuestion || e.Key == Key.OemComma) && e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                e.Handled = true;
                pop.IsOpen = true;
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
